from llvmlite import ir
from .types import double, integer, string
from .global_mgrs import id_generator


class PMMacroGenerator:
    def __init__(self, builder: ir.IRBuilder):
        self.builder = builder

    def int_to_double(self, double_var):
        tmp_id = id_generator.new_id('_tmp_int_to_double')
        var = self.builder.sitofp(double_var, double, name=tmp_id)
        return var

    def double_to_int(self, int_var):
        tmp_id = id_generator.new_id('_tmp_double_to_int')
        var = self.builder.fptosi(int_var, integer, name=tmp_id)
        return var

    def get_size(self, obj_type: ir.LiteralStructType):
        size = self.builder.gep(ir.Constant(ir.PointerType(obj_type), None), [integer(1)])
        sizei = self.builder.ptrtoint(size, integer)
        return sizei